import java.util.Arrays;

class Student {
            String name;
            int age;

            public Student(String name, int age) {
                this.name = name;
                this.age = age;
            }

            @Override
            public String toString() {
                return "Student{" +
                        "name='" + name + '\'' +
                        ", age=" + age +
                        '}';
    }
}
public class Test {
    public static void main(String[] args) {
        //字符串去左右空格
        String str = "  hello world    ";
        String ret = str.trim();
        System.out.println(ret);

        //判断是否包含
        boolean b = str.contains("hello");
        System.out.println(b);
        //判断是否以什么开头结尾
        boolean b1 = ret.startsWith("he");
        boolean b2 = ret.endsWith("ld");
        System.out.println(b1);
        System.out.println(b2);
    }
    public static void main11(String[] args) {
        //字符串截取
        String str = "helloworld";
        String ret = str.substring(1, 4); //截取[1, 4)
        System.out.println(ret);

    }
    public static void main10(String[] args) {
        //字符串分割
        String str = "zhangsan&wangwu&lisi";
        String[] ret = str.split("&");

        System.out.println(Arrays.toString(ret));

        System.out.println("=================");
        String str2 = "168.0.0.1";
        String[] ret2 = str2.split("\\."); //\\为转义字符
        System.out.println(Arrays.toString(ret2));

        System.out.println("=================");
        String str3 = "168\\0\\0\\1";
        String[] ret3 = str3.split("\\\\");
        System.out.println(Arrays.toString(ret3));

        System.out.println("==================");
        String str4 = "name=zhangsan&age=10";
        String[] ret4 = str4.split("=|&"); //多个分隔符
        for(String s : ret4) {
            System.out.println(s);
        }
    }
    public static void main9(String[] args) {
        //字符串替换
        String s1 = "asdasdasdavbabc";
        String ret1 = s1.replace('a', 'n');
        System.out.println(ret1);

        String ret2 = s1.replace("as", "bs");
        System.out.println(ret2);

        String ret3 = s1.replaceAll("as" ,"pk");
        System.out.println(ret3);

        String ret4 = s1.replaceFirst("as", "0");
        System.out.println(ret4);
    }

    public static void main8(String[] args) {
        //转大小写
        String str = "hello";
        String str2 = "HELLO";

        System.out.println(str.toUpperCase());
        System.out.println(str2.toLowerCase());

        System.out.println("=================");
        //字符串转数组
        char[] chars = str.toCharArray();
        System.out.println(Arrays.toString(chars));
        //字符数组转字符串
        String ch = new String(chars);
        //格式化输出字符串
        String ret = String.format("%d-%d-%d", 2023, 3, 30);
        System.out.println(ret);

    }


    public static void main7(String[] args) {
        //转字符串
        String s1 = String.valueOf(123);
        String s2 = String.valueOf(true);
        String s3 = String.valueOf(new Student("常威", 10));

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);

        System.out.println("==========");

        //字符串转
        int a = Integer.parseInt("123");
        System.out.println(a + 1);

        double b = Double.parseDouble("11.2");
        System.out.println(b + 1);
    }
    public static void main6(String[] args) {
        for(int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
    }
    public static void main5(String[] args) {
        String str = "abcdecabcd";

        //遍历打印字符串 使用charAt方法
        for(int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            System.out.print(ch);
        }

        //找字符串/字符串中字符 返回该下标 使用indexOf方法 下标从0开始
        System.out.println(str.indexOf('c'));//找到第一个结束
        System.out.println(str.indexOf('c', 3));//指定从那个下标开始找
        System.out.println(str.indexOf("cde"));
        //从后往前找 lastIndexOf
        System.out.println(str.lastIndexOf('c', 4));
        System.out.println(str.lastIndexOf("cd", 2));
    }
    public static void main4(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");

        //String类实现了接口comparable，并重写了compareTo方法
        //int ret = s1.compareTo(s2);
        int ret = s1.compareToIgnoreCase(s2); //忽略大小写进行比较

        if(ret > 0) {
            System.out.println("s1>s2");
        } else if(ret == 0) {
            System.out.println("s1=s2");
        } else {
            System.out.println("s1<s2");
        }
    }
    public static void main3(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");

        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2)); //String类重写object类中的equals方法
        System.out.println(s1.equalsIgnoreCase(s2)); //忽略大小写进行比较
    }

    public static void main2(String[] args) {
        String s1 = new String("hello");

        String s2 = new String("world");

        String s3 = s1;
        System.out.println(s3);

        System.out.println(s3.length());
    }

    public static void main1(String[] args) {
        //构造字符串对象的方式 三种常见
        String str1 = "hello";

        String str2 = new String("hello");

        char[] chars = {'h', 'e', 'l', 'l', 'o'};
        String str3 = new String(chars);

        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
    }
}
